package com.jeff.tianti.controller;

import com.jeff.tianti.cms.dto.AdvertisingEncyclopediaQueryDTO;
import com.jeff.tianti.cms.dto.AnswersQueryDTO;
import com.jeff.tianti.cms.dto.QuestionsAnswersQueryDTO;
import com.jeff.tianti.cms.dto.font.QuestionsAnswersTo;
import com.jeff.tianti.cms.entity.*;
import com.jeff.tianti.cms.service.*;
import com.jeff.tianti.common.PageBean;
import com.jeff.tianti.common.dto.AjaxResult;
import com.jeff.tianti.common.entity.BaseEntity;
import com.jeff.tianti.common.entity.PageModel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/advertising")//广告百科模块
@CrossOrigin(maxAge = 3600)
public class AdvertisingEncyclopediaController {
    @Autowired
    private AdvertisingEncyclopediaService advService;
    @Autowired
    private AdvertisingEncyclopediaTypeService advTypeServicie;
    @Autowired
    private QuestionsAnswersService questionsAnswersService;
    @Autowired
    private AnswersService answersService;
    @Autowired
    private GateUserService gateUserService;


    /**
     * @Ceator:liuYI
     * @Describe:广告百科模块->百科行业类型(背景图+百科行业类型名称)
     */
    @RequestMapping(value = "/industry/list", method = RequestMethod.GET)
    public AjaxResult findAdvertisingEncyclopediaTypeList() {
        List<AdvertisingEncyclopediaType> list = advTypeServicie.queryAdvertisingEncyclopediaTypeList(null);
        return AjaxResult.setSuccessData(list);
    }

    /**
     * @Ceator:liuYI
     * @param:0->问答类型 1->百科类型
     * @Describe:广告百科模块->问答类型/百科类型列表
     */
    @RequestMapping(value = "/type/list", method = RequestMethod.GET)
    public AjaxResult findAdvertisingEncyclopediaByType(@RequestParam("type") String type) {
        AdvertisingEncyclopediaQueryDTO dto = new AdvertisingEncyclopediaQueryDTO();
        dto.setType(type);
        List<AdvertisingEncyclopedia> list = advService.queryAdvertisingEncyclopediaList(dto);
        return AjaxResult.setSuccessData(list);
    }

    /**
     * @Ceator:liuYI
     * @Describe:广告百科模块->推荐问答列表
     */
    @RequestMapping(value = "/recommend/list", method = RequestMethod.POST)
    public AjaxResult findQuestionsAnswers(@RequestBody PageBean pageBean) {
        QuestionsAnswersQueryDTO dto = new QuestionsAnswersQueryDTO();
        dto.setCurrentPage(pageBean.getCurrentPage());
        dto.setPageSize(pageBean.getPageSize());
        //筛选最新创建的提问进行分页筛选排序
        PageModel<QuestionsAnswers> page = questionsAnswersService.queryQuestionsAnswersPages(dto);
        List<QuestionsAnswers> list = page.getList();
        //根据提问拿到回答内容
        Map<String, Object> map = new HashMap<>();
        list.stream().forEach(t -> {
            List<Answers> answers = answersService.findAllByQuestionsAnswersId(t.getId());
            map.put(t.getText(), answers);
        });
        return AjaxResult.setSuccessData(map);
    }

    @Autowired
    private QuestionsAnswersNumService questionsAnswersNumService;

    /**
     * @Ceator:liuYI
     * @Describe:广告百科模块->我要提问
     */
    @RequestMapping(value = "/quiz", method = RequestMethod.POST)
    public AjaxResult saveQuestionsAnswers(@RequestBody QuestionsAnswers questionsAnswers) {
        questionsAnswers.setState("0");//0->咨询中 1->已完成
        questionsAnswers.setDeleteFlag(BaseEntity.DELETE_FLAG_NORMAL);
        questionsAnswers.setAuditFlag(BaseEntity.AUDIT_FLAG_NOT);//审核不通过
        questionsAnswersService.save(questionsAnswers);
        QuestionsAnswersNum questionsAnswersNum = questionsAnswersNumService.find(questionsAnswers.getUid());
        if (questionsAnswersNum == null || StringUtils.isBlank(questionsAnswersNum.getUid())) {
            questionsAnswersNum = new QuestionsAnswersNum();
            questionsAnswersNum.setUid(questionsAnswers.getUid());
            questionsAnswersNum.setQuestionNum(1);
            questionsAnswersNum.setQuestionDate(new Date());
            questionsAnswersNumService.save(questionsAnswersNum);
        } else {
            int num = questionsAnswersNum.getQuestionNum();
            questionsAnswersNum.setQuestionNum(++num);
            questionsAnswersNum.setQuestionDate(new Date());
            questionsAnswersNumService.update(questionsAnswersNum);
        }
        return AjaxResult.setSuccessData(null);
    }

    /**
     * @Ceator:liuYI
     * @Describe:广告百科模块->我要来回答->推荐你答->我来回答
     */
    @RequestMapping(value = "/reply", method = RequestMethod.POST)
    public AjaxResult saveAnswers(@RequestBody Answers answers) {
        String questionsAnswersId = answers.getQuestionsAnswersId();
        //问题表更新设置问题的状态为已完成（已回复）
        QuestionsAnswers questionsAnswers = questionsAnswersService.find(questionsAnswersId);
        questionsAnswers.setState("1");
        questionsAnswersService.update(questionsAnswers);
        //回复表添加数据
        answers.setReplyDate(new Date());
        answers.setCreateDate(new Date());
        answers.setAuditFlag(BaseEntity.AUDIT_FLAG_FAIL);//审核不通过
        answers.setQueUid(questionsAnswers.getUid());
        answers.setText(questionsAnswers.getText());
        answersService.save(answers);
        QuestionsAnswersNum questionsAnswersNum = questionsAnswersNumService.find(answers.getUid());
        if (questionsAnswersNum == null || StringUtils.isBlank(answers.getUid())) {
            questionsAnswersNum = new QuestionsAnswersNum();
            questionsAnswersNum.setUid(answers.getUid());
            questionsAnswersNum.setAnswersNum(1);
            questionsAnswersNum.setAnswersDate(new Date());
            questionsAnswersNumService.save(questionsAnswersNum);
        } else {
            int num = questionsAnswersNum.getAnswersNum();
            questionsAnswersNum.setAnswersNum(++num);
            questionsAnswersNum.setAnswersDate(new Date());
            questionsAnswersNumService.update(questionsAnswersNum);
        }
        return AjaxResult.setSuccessData(null);
    }

    /**
     * @Ceator:liuYI
     * @param: auditFlag：审核字段 0->待审核 1->审核不通过 2审核通过
     * state：0->咨询中 1->已完成  只要这个问题有被回复
     * @Describe:广告百科模块->我要来回答->推荐你答
     */
    @RequestMapping(value = "/questions/list", method = RequestMethod.POST)
    public AjaxResult findQuestionsAnswers(@RequestBody QuestionsAnswersTo questionsAnswersTo) {
        PageModel<QuestionsAnswers> page = questionsAnswersService.findAllByTo(questionsAnswersTo);
        return AjaxResult.setSuccessPageModelDate(page);
    }

    /**
     * @Ceator:liuYI
     * @Describe:获取用户的提问次数和回答的次数头像,昵称
     */
    @RequestMapping(value = "/findNum", method = RequestMethod.GET)
    // TODO: 2019\7\26 0026 uid写死了
    public AjaxResult findNumByUid(@RequestParam(value = "uid", defaultValue = "402881c26b7949c3016b794e0b8d0001") String uid) {
        QuestionsAnswersNum questionsAnswersNum = questionsAnswersNumService.find(uid);
        GateUser gateUser = gateUserService.find(uid);
        Map<String, Object> map = new HashMap<>();
        map.put("questionNum", 0);
        map.put("answersNum", 0);
        if (questionsAnswersNum != null) {
            map.put("questionNum", questionsAnswersNum.getQuestionNum());
            map.put("answersNum", questionsAnswersNum.getAnswersNum());
        }
        map.put("icon", gateUser.getIcon() == null ? "" : gateUser.getIcon());//头像
        map.put("nickName", gateUser.getNickName() == null ? "" : gateUser.getNickName());//昵称
        return AjaxResult.setSuccessData(map);
    }
}
